import { Body, Controller, Delete, Get, Post, Query } from '@nestjs/common';
import { Like } from 'typeorm';
import { RequirePermission } from '../../../common/decorators/require-permission.decorator';
import {
  PageQueryParam,
  PaginationPipe,
} from '../../../common/pipes/pagination.pipe';
import { DictService } from './dict.service';
import { SaveDictItemDto } from './dto/dict-item.dto';
import { SaveDictTypeDto } from './dto/dict-type.dto';

@Controller('dict')
export class DictController {
  constructor(private readonly dictService: DictService) {}

  @RequirePermission('sys:dict:save')
  @Post('save-type')
  async save(@Body() body: SaveDictTypeDto) {
    return await this.dictService.saveDictType(body);
  }

  @RequirePermission('sys:dict:remove')
  @Delete()
  async remove(@Body('ids') ids: string[]) {
    return await this.dictService.softDeleteDictType(ids);
  }

  @RequirePermission('sys:dict:find')
  @Get('find-page')
  async findPage(
    @Query(PaginationPipe) param: PageQueryParam,
    @Query('name') name?: string,
    @Query('code') code?: string,
  ) {
    return await this.dictService.findAndCountDictType({
      where: { name: name ? Like(`%${name}%`) : undefined, code },
      relations: {
        items: true,
      },
      ...param,
    });
  }

  @RequirePermission('sys:dict:save')
  @Post('save-item')
  async saveItem(@Body() body: SaveDictItemDto) {
    return await this.dictService.saveDictItem(body);
  }

  @RequirePermission('sys:dict:remove')
  @Delete('remove-item')
  async removeItem(@Body('ids') ids: string[]) {
    return await this.dictService.softDeleteDictItem(ids);
  }

  @Get('get-dict')
  async findOne(@Query('filterKey') filterKey: string) {
    return await this.dictService.findDictType(filterKey);
  }
}
